<?php

require_once 'base.php';

global $babAddonPhpPath;

include_once $GLOBALS['babInstallPath'] . "utilit/loginIncl.php";

include_once $babAddonPhpPath . '../UNSDepartement/function.php';

include_once $babAddonPhpPath . '/defines.php';

include_once $babAddonPhpPath . '/functions/funcBuilding.php';
include_once $babAddonPhpPath . '/functions/funcClassroom.php';
include_once $babAddonPhpPath . '/functions/funcLecture.php';
include_once $babAddonPhpPath . '/functions/funcLectureType.php';
include_once $babAddonPhpPath . '/functions/funcWorkPraticeGroupCalendar.php';

function unsCalendar_getRessourceCalendarIDForName($name) {
    global $babDB;

    $res = $babDB->db_query("select * from " . BAB_CAL_RESOURCES_TBL . " where name = '$name'");
    $count = $babDB->db_num_rows($res);

    if ($count == 1) {
        $arr = $babDB->db_fetch_array($res);
        return $arr;
    } else if ($count == 0) {
        return 0;
    } else
        return 2;
}

function unsCalendar_getPublicCalendarIDForName($name) {
    global $babDB;

    $res = $babDB->db_query("select * from " . BAB_CAL_PUBLIC_TBL . " where name = '$name'");
    $count = $babDB->db_num_rows($res);

    if ($count == 1) {
        $arr = $babDB->db_fetch_array($res);
        return $arr;
    } else if ($count == 0) {
        return 0;
    } else
        return 2;
}

function unsCalendar_generateUID() {
    $string = "";
    $chaine = "abcdefghijklmnpqrstuvwxy";
    srand((double) microtime() * 1000000);
    for ($i = 0; $i < 10; $i++) {
        $string .= $chaine[rand() % strlen($chaine)];
    }
    return $string;
}

function unsCalendar_getUser($id) {


    global $babDB;

    $res = $babDB->db_query("select * from " . BAB_USERS_TBL . " where id = '$id'");
    $count = $babDB->db_num_rows($res);

    if ($count == 1) {
        $arr = $babDB->db_fetch_array($res);
        return $arr;
    }
    return false;
}

function unsCalendar_calendarIsDisponible($idCal, $startDate, $endDate)
{
    global $babDB;
    //echo "SELECT * FROM ".BAB_CAL_EVENTS_TBL.", ".BAB_CAL_EVENTS_OWNERS_TBL." WHERE ".BAB_CAL_EVENTS_TBL.".id = id_event AND id_cal = $idCal AND (start_date BETWEEN '$startDate' AND '$endDate') OR (end_date BETWEEN '$startDate' AND '$endDate') OR ( start_date < '$startDate' AND  end_date > '$endDate')";
    $res = $babDB->db_query("SELECT * FROM ".BAB_CAL_EVENTS_TBL.", ".BAB_CAL_EVENTS_OWNERS_TBL." WHERE ".BAB_CAL_EVENTS_TBL.".id = id_event AND id_cal = $idCal AND (start_date BETWEEN '$startDate' AND '$endDate') OR (end_date BETWEEN '$startDate' AND '$endDate') OR ( start_date < '$startDate' AND  end_date > '$endDate')");
    
    return $babDB->db_num_rows($res);
 
}

function unsCalendar_staffCalendar($id)
{
    global $babDB;
    
    $res = $babDB->db_query("SELECT * FROM ".BAB_CALENDAR_TBL." WHERE owner = $id AND type = 1");
    
    $arr = $babDB->db_fetch_array($res);
    
    return $arr[0];
}

?>
